home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / src / phone / ph_slave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  7.3 KB  |  287 lines

  1. #include "util.h"
  2. #include "mmdf.h"
  3.  
  4. /*
  5.  *     MULTI-CHANNEL MEMO DISTRIBUTION FACILITY  (MMDF)
  6.  *     
  7.  *
  8.  *     Copyright (C) 1979,1980,1981  University of Delaware
  9.  *     
  10.  *     Department of Electrical Engineering
  11.  *     University of Delaware
  12.  *     Newark, Delaware  19711
  13.  *
  14.  *     Phone:  (302) 738-1163
  15.  *     
  16.  *     
  17.  *     This program module was developed as part of the University
  18.  *     of Delaware's Multi-Channel Memo Distribution Facility (MMDF).
  19.  *     
  20.  *     Acquisition, use, and distribution of this module and its listings
  21.  *     are subject restricted to the terms of a license agreement.
  22.  *     Documents describing systems using this module must cite its source.
  23.  *
  24.  *     The above statements must be retained with all copies of this
  25.  *     program and may not be removed without the consent of the
  26.  *     University of Delaware.
  27.  *     
  28.  *
  29.  *     version  -1    David H. Crocker    March   1979
  30.  *     version   0    David H. Crocker    April   1980
  31.  *     version  v7    David H. Crocker    May     1981
  32.  *     version   1    David H. Crocker    October 1981
  33.  *
  34.  */
  35. /*
  36.  *  PHONENET SLAVE PROCESS
  37.  *
  38.  *  Handles telephone link & mail protocols, interfacing to MMDF for
  39.  *  submission and pickup, via the mm_ package.
  40.  *
  41.  *  One argument is required:  The name of the channel the caller is
  42.  *  claiming to come in from.  (It may be possible for one username to have
  43.  *  access to mail for more than one channel, so the caller must indicate
  44.  *  the channel of interest, for pickup.  However, Submit and/or Ch_PoBox
  45.  *  will verify the claim.)
  46.  *
  47.  *  Sep 81  D. Crocker      allow reading channel name as first input line
  48.  *  Nov 81  D. Crocker      commands case-insensitive
  49.  *  Apr 82  D. Crocker      check chan name; have llog hdr show chan char
  50.  *  Feb 84  D. Long        made full channel name show in llog hdr
  51.  *  Apr 84  D. Long         fixed phs_note call after pickup 
  52.  */
  53.  
  54. #include <signal.h>
  55. #include "ch.h"
  56. #include "phs.h"
  57.  
  58. #define CTIMEOUT    180    /* 3 minute timeout on response to channel prompt */
  59.  
  60. extern char *dupfpath ();
  61. extern LLog *logptr;
  62.  
  63. extern jmp_buf timerest;           /* return location saved for timeout  */
  64. extern int     flgtrest;
  65.  
  66. char    ttyobuf[BUFSIZ];
  67.  
  68. LOCVAR struct rp_construct        /* replies                            */
  69.         rp_unknown =
  70. {
  71.     RP_PROT, 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 'p', 'r', 'o',
  72.     't', 'o', 'c', 'o', 'l', ' ', 'c', 'o', 'm', 'm', 'a', 'n', 'd', '\0'
  73. }          ,
  74.         rp_sbok =
  75. {
  76.     RP_OK, 's', 'e', 'n', 'd', ' ', 'o', 'k', '\0'
  77. }          ,
  78.         rp_pkok =
  79. {
  80.     RP_OK, 'p', 'i', 'c', 'k', 'u', 'p', ' ', 'o', 'k', '\0'
  81. };
  82.  
  83.  
  84. /*    MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN      */
  85.  
  86. main (argc, argv)
  87. int       argc;
  88. char   *argv[];
  89. {
  90.     Chan *curchan;
  91.     char chaname[64];
  92.  
  93.     umask(0);
  94.  
  95.     setbuf (stdout, ttyobuf);
  96.     mmdf_init (argv[0]);
  97.  
  98.     siginit ();
  99.     if (argv[0][0] == '-')
  100.     {                             /* if login shell, ignore quit & int  */
  101.     signal (SIGINT, SIG_IGN);
  102.     signal (SIGQUIT, SIG_IGN);
  103.     }
  104.  
  105.     if (setjmp (timerest) != 0)   /* other side took too long; give up    */
  106.     {
  107.     flgtrest = FALSE;
  108.     err_abrt (RP_TIME, "Timeout");
  109.     }
  110.     else
  111.     flgtrest = TRUE;
  112.  
  113.     if (argc == 1)                /* channel name not specified as arg  */
  114.     {
  115.     s_alarm (CTIMEOUT);
  116.     printf ("channel: ");
  117.     fflush (stdout);
  118.     gets (chaname);           /* read it                            */
  119.     s_alarm (0);
  120.     }
  121.     else                          /* use the argument                   */
  122.     strcpy (chaname, argv[1]);
  123.  
  124.     ll_hdinit (logptr, chaname); /* which channel are we?    */
  125.  
  126.     if ((curchan = ch_nm2struct (chaname)) == (Chan *) NOTOK)
  127.     err_abrt (RP_PARM, "Channel '%s' is unknown", chaname);
  128.  
  129.     ph_slave (curchan);
  130. }
  131.  
  132. /*  *****************  SERVER/SLAVE MODE  ****************************** */
  133.  
  134. ph_slave (curchan)                /* service requests from caller       */
  135.     Chan *curchan;
  136. {
  137.     int       len;
  138.     short     retval;
  139.     char    linebuf[LINESIZE];
  140.     register char *ptr;
  141.  
  142.     ch_llinit (curchan);
  143.     if (rp_isbad (retval = ph_init (curchan)))
  144.     err_abrt (retval, "Error with initialization");
  145.                   /* set-up to service requests         */
  146.  
  147.     FOREVER
  148.     {                  /* what does caller want to do?       */
  149.     if (rp_isbad (retval = ph_rrec (linebuf, &len)))
  150.         err_abrt (retval, "Error return getting command");
  151.  
  152. #ifdef DEBUG
  153.     ll_log (logptr, LLOGFTR, "Command: \"%s\"", linebuf);
  154. #endif
  155.  
  156.     for (ptr = linebuf; !isnull (*ptr); ptr++)
  157.         *ptr = uptolow (*ptr);
  158.  
  159.     if (len == 0 ||          /* all done?                          */
  160.         equal (linebuf, "end", 3) ||
  161.         rp_gval (retval) == RP_DONE)
  162.         break;          /* exit                               */
  163.  
  164.     if (equal (linebuf, "submit", 6))
  165.     {              /* gonna RECEIVE mail                 */
  166.         sl_submit (curchan);
  167.         continue;
  168.     }
  169.     if (equal (linebuf, "pickup", 6))
  170.     {              /* gonna SEND mail                    */
  171.         sl_pickup (curchan);
  172.         continue;
  173.     }
  174.  
  175.     ph_wrply ((struct rp_bufstruct *) &rp_unknown,
  176.                         rp_conlen (rp_unknown));
  177.     err_abrt (rp_unknown.rp_cval, "%s:  '%s'",
  178.             rp_unknown.rp_cline, linebuf);
  179.     }
  180.     ph_end (OK);
  181.     mm_end (OK);
  182.     exit (RP_DONE);
  183. }
  184. /* */
  185.  
  186. sl_submit (curchan)               /* dm_slave submission management     */
  187.     Chan *curchan;
  188. {
  189.     short     retval;
  190.  
  191. #ifdef DEBUG
  192.     ll_log (logptr, LLOGBTR, "[ Submit ]");
  193. #endif
  194.  
  195.     if (rp_isbad (retval = ph_wrply ((struct rp_bufstruct *) &rp_sbok,
  196.                 rp_conlen (rp_sbok))))
  197.             err_abrt (retval, "Unable to ack submission request");
  198.  
  199.     phs_note (curchan, PHS_RESTRT);
  200.  
  201.     if (rp_isbad (retval = ph2mm_send (curchan)))
  202.     err_abrt (retval, "ph2mm_send error");
  203.  
  204.     phs_note (curchan, PHS_REEND);
  205. }
  206.  
  207. sl_pickup (curchan)               /* dm_slave pickup management         */
  208.     Chan *curchan;
  209. {
  210.     short     retval;
  211.  
  212. #ifdef DEBUG
  213.     ll_log (logptr, LLOGBTR, "[ Pickup ]");
  214. #endif
  215.  
  216.     if (rp_isbad (retval = ph_wrply ((struct rp_bufstruct *) &rp_pkok,
  217.                 rp_conlen (rp_pkok))))
  218.             err_abrt (retval, "Unable to ack pickup request");
  219.  
  220.     if (rp_isbad (retval = mm2ph_send (curchan)))
  221.     err_abrt (retval, "mm2ph_send error");
  222. }
  223.  
  224. /*  *************************  UTILITIES  ****************************** */
  225.  
  226. /* VARARGS2 */
  227.  
  228. err_abrt (code, fmt, b, c, d)
  229. int       code;
  230. char   *fmt,
  231.        *b,
  232.        *c,
  233.        *d;
  234. {
  235.     if (fmt != 0)
  236.     err_gen (code, fmt, b, c, d);
  237.  
  238.     ph_end (NOTOK);
  239.  
  240.     ll_log (logptr, LLOGFAT, "Ending [%s]", rp_valstr (code));
  241.  
  242.     ll_close (logptr);            /* put out "cycled" msg, if needed    */
  243.  
  244. #ifdef DEBUG
  245.     if (rp_gbval (code) == RP_BTNO && rp_gcval (code) == RP_CCON)
  246.     exit (code);          /* connection-related temp error      */
  247.  
  248.     abort ();
  249. #else
  250.     exit (code);
  251. #endif
  252. }
  253. /* */
  254.  
  255. /* VARARGS2 */
  256.  
  257. err_gen (code, fmt, b, c, d)      /* standard error processing          */
  258. int       code;                     /* see err_abrt, for explanation      */
  259. char   *fmt,
  260.        *b,
  261.        *c,
  262.        *d;
  263. {
  264.     extern int    errno;
  265.     char newfmt[LINESIZE];
  266.  
  267.     if (rp_isgood (code))      /* it wasn't an error                 */
  268.     return;
  269.  
  270.     printf (fmt, b, c, d);        /* the user what the problem is       */
  271.     putchar ('\n');
  272.     fflush (stdout);
  273.  
  274.     mm_end (NOTOK);
  275.     switch (code)          /* log the error?                     */
  276.     {
  277.     case RP_HUH:           /* not if it was a user error         */
  278.     case RP_PARM: 
  279.     case RP_USER: 
  280.         break;
  281.  
  282.     default: 
  283.         sprintf (newfmt, "%s%s", "err [ ABEND (%s) ] ", fmt);
  284.         ll_err (logptr, LLOGFAT, newfmt, rp_valstr (code), b, c, d);
  285.     }
  286. }
  287.